package cn.lixu._03oneWay;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeOrderlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeOrderlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerOrderly;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.common.message.MessageExt;

import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * @author LiXu
 * @date 2022/08/18 0018 0:08:44
 */
public class Consumer_1 {
    public static void main(String[] args) throws MQClientException {
        //1.创建连接，获取对象
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("oneway_consumer");
        //2.设置连接地址值
        consumer.setNamesrvAddr("localhost:9876");
        //3.订阅消息
        consumer.subscribe("hello_chengdu", "chengdu");
        //4.注册监听器
        consumer.registerMessageListener(new MessageListenerOrderly() {
            @Override
            public ConsumeOrderlyStatus consumeMessage(List<MessageExt> list, ConsumeOrderlyContext consumeOrderlyContext) {
                for (MessageExt messageExt : list) {
                    System.out.println(new String(messageExt.getBody(), StandardCharsets.UTF_8));
                }
                return ConsumeOrderlyStatus.SUCCESS;
            }
        });
        //5.启动
        consumer.start();
    }
}
